/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             * 
 *              GNU Lesser General Public Licence (LGPL) version 3,             *  
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
#ifndef HIMUSICWAVEFORM_H
#define HIMUSICWAVEFORM_H
#include "TH1F.h"
#include "TGraph.h"
extern TH1F *h;

class HiMusicWaveForm : public TObject
{
private:
	Int_t fNPoints;
	Float_t fTStep;
	Float_t fData[4096];
	Bool_t fType; //// Anode signal: fType = 0; Cathode signal: fType=1

public:
	HiMusicWaveForm();
	HiMusicWaveForm(Int_t NP, Float_t dt, Float_t data = 0, Bool_t t0 = 0);
	HiMusicWaveForm(const HiMusicWaveForm &w);
	~HiMusicWaveForm() {}
	void SetNPoints(Int_t n) { fNPoints = n; }
	void SetTStep(Float_t dt) { fTStep = dt; }
	void SetType(Bool_t t) { fType = t; }
	void SetBaseLine(Float_t data);

	void Reset();
	void Print();
	void Print(Int_t Nstart, Int_t Nstop);

	// 绘制波形
	void Plot(Int_t ncolor = 1, TString option = "", TString type = "time");
	Int_t GetNumOfPoints() { return fNPoints; }
	Float_t GetTStep() { return fTStep; }
	Bool_t GetType() { return fType; }
	Float_t GetData(Int_t i);
	Float_t *GetData() { return fData; }
	void SetData(Float_t *data);
	void SetData(std::vector<uint16_t> data);
	void SetData(Int_t i, Float_t data);
	TH1F *MakeHist(const char *name = "hwave", TString flag = "time");
	TGraph *MakeGraph(const char *name = "gwave", TString type = "time");
	TH1F *MakeHistWithRange(const char *name = "hwave", TString flag = "time", Float_t xmin = 0, Float_t xmax = 1000);
	void Inverse();
	void InverseWithPed(Float_t ped);

	void Smooth(Int_t n);
	Float_t GetPedSlope(Int_t Npoints, TString flag = "time");
	Float_t GetPedestal(Int_t Npoints = 150);
	void Add(HiMusicWaveForm &w);
	void Substract(HiMusicWaveForm &w);
	void AddPed(Float_t ped);
	void SubstractPed(Float_t ped);
	Float_t GetSum(Float_t th = 0);
	Float_t GetPeak();
	TH1F *GetDiff(Int_t npoint = 1);
	TH1F *GetDiff(TH1F *hh);
	Float_t GetMaximum();
	Float_t GetMinimum();
	Float_t GetMinimum(Float_t tlow, Float_t thigh);
	TH1F *GetSigma(TH1F *hh);

	Float_t SlowFilter(Int_t fl, Int_t fg, Float_t tau);								   // 梯形成形
	Float_t GaussFilter(Int_t k, Float_t baseline = 0);									   // S-K滤波器
	Float_t PzcCircuit(Float_t m_k1 = 0.005, Float_t m_k2 = 0.05, Float_t baseline = 820); // 极零相消
	Float_t CRCircuit(Float_t m_d = 0.95, Float_t baseline = 0);						   // CR高通微分电路
	Float_t RCCircuit(Float_t m_d = 0.95, Float_t baseline = 0);						   // RC低通积分电路
	Float_t GMM_EM(Int_t m_Point, Int_t m_k = 2);										   // EM算法
	HiMusicWaveForm ReSample(Float_t dt, Int_t Nfrom = 0, Int_t Nto = -1);

	ClassDef(HiMusicWaveForm, 1);
};

#endif //HIMUSICWAVEFORM_H
